package com.onePractice.spring.demo.controller;

import com.onePractice.spring.demo.model.UserInfo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
public class RequestController {

    @RequestMapping("/r1")
    public String r1(String keyword) {
        return "接收参数: " + keyword;
    }

    @RequestMapping("/r2")
    public String r2(String userName,String password) {
        return "接收参数: " +userName + ",password: " + password;
    }

    //包装类如果没有传入值，或者没有构成映射关系，都会传入 null
    @RequestMapping("/r3")
    public String r3(Integer number) {
        return "接受参数: " + number;
    }

    //基本类型如果没有传入值，或者没有构成映射关系，都会报错 500
    @RequestMapping("/r4")
    public String r4(int number) {
        return "接收参数: " + number;
    }

    //传递数组
    @RequestMapping("/r5")
    public String r5(int[] arr) {
        return "接收参数: arr = " + Arrays.toString(arr);
    }

    //如果是用没有构造方法或有无参的构造方法的对象接收的话，int 是可以不传入值得
    //不传入值是，int 为 0
    @RequestMapping("/r6")
    public String r6(UserInfo userInfo) {
        return "接收参数: UserInfo = " + userInfo.toString();
    }

    //将客户端传入的 "q" 与 自己设定的 keyword 绑定
    @RequestMapping("/r7")
    public String r7(@RequestParam("q") String keyword) {
        return "接受参数: " + keyword;
    }

    @RequestMapping("/r8")
    public String r8(@RequestParam List<Integer> list) {
        return "接受参数: list = " + list;
    }

    //传递JOSN
    @RequestMapping("r9")
    public String r9(@RequestBody UserInfo userInfo) {
        return userInfo.toString();
    }

    //从URL中获取数据
    @RequestMapping("/article/{articleID}")
    public String r10(@PathVariable Integer articleID) {
        return "获取文章ID: " + articleID;
    }

    //获取两个数据
    @RequestMapping("/article/{type}/{articleID}")
    public String r11(@PathVariable Integer articleID,@PathVariable("type") String articleType) {
        return "获取文章ID: " + articleID + " 文章类型: " + articleType;
    }

    //上传文件
    @RequestMapping("/r10")
    public String r10(@RequestPart("file11") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        //文件上传
        file.transferTo(new File("D://" + file.getOriginalFilename()));
        return "文件上传成功！";
    }

    @RequestMapping("/r11")
    public String r11(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if(cookies != null) {
            for(Cookie cookie : cookies) {
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            }
        }
        return "返回Cookie成功";
    }

    @RequestMapping("/r12")
    public String r12(@CookieValue("name") String name) {
            return "从Cookie中获得class的值: " + name;
    }

    //存储session
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        //从Cookie中获得SessionID,通过SessionID 获得Session对象
        HttpSession session = request.getSession();
        //默认存储在内存里
        //登录的用户名称
        session.setAttribute("userName","zhangsan");
        session.setAttribute("age",19);
        return "设置Session成功";
    }
    //获得 session
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        //从Cookie中获得SessionID,通过SessionID 获得Session对象
        HttpSession session = request.getSession(false);
        //如果用户登录，session有值，如果未登录，session为null
        if (session == null) {
            return "用户未登录";
        }else {
            //从Session中获得登录用户的信息
            String userName = (String)session.getAttribute("userName");
            return "登录用户为: " + userName;
        }
    }

    //获得 session
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session) {
            //从Session中获得登录用户的信息
            String userName = (String)session.getAttribute("userName");
            return "登录用户为: " + userName;
    }

    //获得 session
    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute("userName") String userName) {
        return "登录用户为: " + userName;
    }

    //获取Header
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request) {
        String userAgent = request.getHeader("user-Agent");
        return "从Header中获得userAgent为: " + userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader String userAgent) {
        return "从Header中获得userAgent为: " + userAgent;
    }
}
